<?php

namespace App\Models\Traits;

use App\Jobs\CrawlerPictuerJobs;

trait CrawlerPictuerHelper
{
    // 抓取地址
    /*
    public function crawler()
    {
        $pictuer = $this->where([
            ['crawler', '=', 0],
            ['download_count', '<', 4],
            ['oss', null]
        ])->first();
        if ($pictuer) {
            dispatch(new CrawlerPictuerJobs($pictuer));
            return $pictuer;
        }
    }
    */
    public function crawler()
    {
        $pictuers = $this->where([
            ['crawler', '=', 0],
            ['download_count', '<', 4],
            ['oss', null]
        ])->limit(100)->get();
        if (count($pictuers) > 0) {
            foreach ($pictuers as $pictuer) {
              dispatch(new CrawlerPictuerJobs($pictuer));
            }
        }
        return $pictuers;
    }
    public function findByMd5($md5 = null)
    {
      if($md5) return $this->where('md5', $md5)->first();
      else return false;
    }
}
